//
//  HJVideoPlayController.m
//  KaiChat
//
//  Created by lx on 2024/3/25.
//

#import "HJVideoPlayController.h"
#import "ZFIJKPlayerManager.h"
#import "ZFPlayerControlView.h"
#import "ZFPlayerConst.h"
#import "ZFUtilities.h"
#import "UIView+ZFFrame.h"


@interface HJVideoPlayController ()

@property (nonatomic, strong) ZFPlayerController *player;
@property (nonatomic, strong) UIImageView *containerView;
@property (nonatomic, strong) ZFPlayerControlView *controlView;

@end

@implementation HJVideoPlayController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationBar.alpha = 0;
    self.view.backgroundColor = UIColor.blackColor;
    [self.navigationBar.backBarButton whiteColor];
    [self.view addSubview:self.containerView];
    [self setupPlayer];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.player.viewControllerDisappear = NO;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.player.viewControllerDisappear = YES;
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
 
    self.containerView.frame = CGRectMake(0, KStatusBarHight, self.view.bounds.size.width, self.view.bounds.size.height - KStatusBarHight - TabbarSafeBottomMargin);
}

- (void)setupPlayer {
    ZFIJKPlayerManager *playerManager = [[ZFIJKPlayerManager alloc] init];
    playerManager.shouldAutoPlay = YES;
    
    /// 播放器相关
    self.player = [ZFPlayerController playerWithPlayerManager:playerManager containerView:self.containerView];
    self.player.controlView = self.controlView;
    /// 设置退到后台继续播放
    self.player.pauseWhenAppResignActive = NO;
//    self.player.resumePlayRecord = YES;
    
    @zf_weakify(self)
    self.player.orientationDidChanged = ^(ZFPlayerController * _Nonnull player, BOOL isFullScreen) {
        /* // 使用YYTextView转屏失败
        for (UIWindow *window in [UIApplication sharedApplication].windows) {
            if ([window isKindOfClass:NSClassFromString(@"YYTextEffectWindow")]) {
                window.hidden = isFullScreen;
            }
        }
        */
    };
    /// 播放完成
    self.player.playerDidToEnd = ^(id  _Nonnull asset) {
        @zf_strongify(self)
        if (!self.player.isLastAssetURL) {
            [self.player playTheNext];
        } else {
            [self.player stop];
        }
    };
    
    if (_localPath && [[NSFileManager defaultManager] fileExistsAtPath:_localPath]) {
        NSLog(@"播放本地视频");
        self.player.assetURL = [NSURL fileURLWithPath:_localPath];
    } else {
        if (_url) {
            NSLog(@"加载网络视频");
            self.player.assetURL = _url.URL;
        }
    }
  
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleDefault;
}

- (BOOL)prefersStatusBarHidden {
    return NO;
}

- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation {
    return UIStatusBarAnimationNone;
}

- (BOOL)shouldAutorotate {
    return NO;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

- (UIModalPresentationStyle)modalPresentationStyle {
    return UIModalPresentationFullScreen;
}

- (ZFPlayerControlView *)controlView {
    if (!_controlView) {
        _controlView = [ZFPlayerControlView new];
        _controlView.fastViewAnimated = YES;
        _controlView.autoHiddenTimeInterval = 5;
        _controlView.autoFadeTimeInterval = 0.5;
        _controlView.prepareShowLoading = YES;
        _controlView.prepareShowControlView = NO;
        _controlView.showCustomStatusBar = YES;
        _controlView.effectViewShow = NO;
    }
    return _controlView;
}

- (void)popViewController {
    [super popViewController];
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (UIImageView *)containerView {
    if (!_containerView) {
        _containerView = [UIImageView new];
    }
    return _containerView;
}

- (void)dealloc {
    NSLog(@"%s", __func__);
}
 
@end
